package com.hst.capacity.infra.persistence.jpa.driver;

import com.hst.capacity.domain.model.entity.driver.DriverLineBandEntity;
import com.hst.capacity.domain.model.response.driver.DriverBandLineVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface DriverLineBandRepository extends JpaRepository<DriverLineBandEntity, String> {

    /**
     * 根据加盟商ID与线路ID获得绑定司机列表
     * @return
     */
    @Query("select new com.hst.capacity.domain.model.response.driver.DriverBandLineVO(d,dp) " +
            "from DriverEntity d " +
            "inner join FranchiseeEntity f on d.idFranchisee = f.idFranchisee " +
            "left join DriverProfileEntity dp on d.idDriverProfile = dp.idDriverProfile " +
            "left join DriverLineBandEntity db on d.idDriver = db.idDriver " +
            "and db.idCityLine=:idCityLine " +
            "where f.idFranchisee=:idFranchisee "+
            "and db.idDriver is not null " )
    List<DriverBandLineVO> findBandDriverListByIdCityLine(String idFranchisee, String idCityLine);

    /**
     * 根据加盟商ID与线路ID获得未绑定司机列表
     * @return
     */
    @Query("select new com.hst.capacity.domain.model.response.driver.DriverBandLineVO(d,dp) " +
            "from DriverEntity d " +
            "inner join FranchiseeEntity f on d.idFranchisee = f.idFranchisee " +
            "left join DriverProfileEntity dp on d.idDriverProfile = dp.idDriverProfile " +
            "left join DriverLineBandEntity db on d.idDriver = db.idDriver " +
            "and db.idCityLine=:idCityLine " +
            "where f.idFranchisee=:idFranchisee "+
            "and db.idDriver is null " )
    List<DriverBandLineVO> findUnBandDriverListByIdCityLine(String idFranchisee, String idCityLine);

    void deleteByIdDriver(String idDriver);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("delete from DriverLineBandEntity d where d.idDriver = ?1 and d.idCityLine = ?2")
    void deleteByIdDriverAndIdCityLine(String idDriver, String idCityLine);

}